package cn.chencq.config;

import javax.servlet.DispatcherType;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;

import cn.chencq.filter.CorsFilter;
import cn.chencq.filter.RequestFilter;

/**
 * Filter配置
 * 
 * @author chencq
 * @email chenchaoqun@incar100.com
 * 
 */
@Configuration
public class FilterConfig {
	
	@Bean
	public FilterRegistrationBean shiroFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new DelegatingFilterProxy("shiroFilter"));
		// 该值缺省为false，表示生命周期由SpringApplicationContext管理，设置为true则表示由ServletContainer管理
		registration.addInitParameter("targetFilterLifecycle", "true");
		registration.setEnabled(true);
		registration.setOrder(Integer.MAX_VALUE - 3);
		registration.addUrlPatterns("/*");
		return registration;
	}

	@Bean
	public FilterRegistrationBean corsFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setDispatcherTypes(DispatcherType.REQUEST);
		registration.setFilter(new CorsFilter());
		registration.setAsyncSupported(true);
		// 该值缺省为false，表示生命周期由SpringApplicationContext管理，设置为true则表示由ServletContainer管理
		registration.addInitParameter("targetFilterLifecycle", "true");
		registration.setName("corsFilter");
		registration.addUrlPatterns("/*");
		registration.setOrder(Integer.MAX_VALUE-2);//从小到大的顺序来依次过滤
		return registration;
	}

	@Bean
	public FilterRegistrationBean requestFilterRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new RequestFilter());
		registration.setName("requestFilter");
		registration.addUrlPatterns("/*");
		registration.setOrder(Integer.MAX_VALUE-1);
		return registration;
	}
}
